package de.hdu.myjobapp.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import de.hdu.myjobapp.model.Application;
import de.hdu.myjobapp.model.Organisation;
import de.hdu.myjobapp.model.User;
import de.hdu.myjobapp.view.MyJobApplication;

//Data Access class for Applicaton model
public class ApplicationDao extends HibernateDaoSupport{
	//create a new Application
	public ApplicationDao(){
		
	}
	/**
	 * create a new Application
	 * @param User - reference to the userobject
	 * @param Organisation - reference to the organisationobject
	 * @param Position - simple string
	 * @param contactPerson - simple string
	 * @param sendate - date - when did the user send his application
	 * @param sourcedate - date format when has the application been pubished
	 * @param applicationsource - simple string
	 * @param description - a string field for up to 3000 chars, editable in a richtextarea
	 * @param answer - wether your application has been accepted or not
	 */
	public void create(String position, String contactPerson, Date SendDate, Date SourceDate, String Description, String Source, Organisation orgid, User userid){
		Application a = new Application(position, contactPerson, SendDate, SourceDate, Description, Source, orgid, userid);
		//Application a = new Application(position,contactPerson, SendDate, SourceDate, Description, Answer);
		save(a);
	}
	/**
	 * Saves the new or updated recieved Application from Method create with hibernate into database
	 * @param Application
	 * @return Application
	 */
	public Application save(Application application){
		HibernateTemplate template = getHibernateTemplate();
		template.saveOrUpdate(application);
		return application;
	}
	//return all existing Applications
	/**
	 * return all existing Applications
	 */
	public List<Application> findAll(){
		HibernateTemplate template = getHibernateTemplate();
		return template.loadAll(Application.class);
	}

	/**
	 * Return an existing Application by naming ID
	 */	
	public Application findById(int id){
		HibernateTemplate template = getHibernateTemplate();
		return(Application) template.get(Application.class, id);
	}
	/**
	 * Find all applications for a specific user
	 * TODO - improve process not to read whole sql table
	 */
	public List<Application> findAll(User user){
		HibernateTemplate template = getHibernateTemplate();
		List<Application> userapplications = new ArrayList<Application>();
		List<Application> allapplications = template.loadAll(Application.class);
		for(Application application:allapplications){
			if(application.getUserId().getUserId().equals(user.getUserId())){
				
				userapplications.add(application);
			}
		}
		return userapplications;
	}
	/**
	 * Alternative Method for aquiring only userdependent applicationobjects
	 * with getting the userId from the application-set user
	 * TODO - not working query atm
	 */
	public List<Application> finduserapplications(){
		HibernateTemplate template = getHibernateTemplate();
		Query query = getSession().createQuery("from Application where User.userId = ?");
		query.setInteger(0, ((User)MyJobApplication.getProject().getUser()).getUserId());
		return query.list();
	}
}
